Skip to main content

Get CPU Serial Number (Linux)

Description

The get_cpu_serial_number_linux function retrieves the CPU serial number on a Linux system by reading the /proc/cpuinfo file. If an error occurs, it returns the exception message.

Function Signature:

def get_cpu_serial_number_linux() -> str:

Parameters

This function does not take any parameters.

Returns

  • str: The CPU serial number if successful. If an error occurs, it returns the error message as a string.

Example Usage

Here’s an example of how the function works:

serial_number = get_cpu_serial_number_linux()
print(serial_number)

Example Output:

000000000023e53d

Code

def get_cpu_serial_number_linux() -> str:
try:
with open('/proc/cpuinfo', 'r') as f:
for line in f:
if line.strip().startswith("Serial"):
return line.strip().split(":")[1].strip()
except Exception as ex:
return str(ex)

Errors and Exceptions

  • If an error occurs while trying to fetch the CPU serial number, the exception message will be returned.

Notes

This function is designed to work on Linux-based systems where the CPU serial number can be found in the /proc/cpuinfo file. If the serial number isn't found, the function will return an empty string or an error message.